package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public Cart add(Cart cart) {

        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        //相当于动态SQL
        //如果菜品名不为空，就查询菜品信息
        wrapper.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId());
        //如果套餐名不为空就查询套餐信息
        wrapper.eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId());
        //指定向哪个用户添加购物车
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        Cart cartFromDB = cartMapper.selectOne(wrapper);

        if (cartFromDB != null) {//数据库查询到了（已经有过同类型订单）
            //给数据库里的订单数+1
            cartFromDB.setNumber(cartFromDB.getNumber() + 1);
            //将更新后的数据进行保存
            cartMapper.updateById(cartFromDB);
            //将更新后的结果返回
            return cartFromDB;
        } else {  //数据库中尚不存在
            //往传入的对象补齐数据
            cart.setUserId(UserHolder.get().getId());
            cart.setNumber(1);
            cart.setCreateTime(new Date());
            //将补齐数据之后的数据插入数据库
            cartMapper.insert(cart);
            return cart;
        }
    }

    /*根据user查询购物车*/
    @Override
    public List<Cart> findList() {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        List<Cart> carts = cartMapper.selectList(wrapper);
        return carts;
    }

    /*清空购物车*/
    @Override
    public void clean() {

        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        cartMapper.delete(wrapper);
    }

    /*更新购物车*/
    @Override
    public Cart update(Cart cart) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId());
        wrapper.eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId());
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        Cart cart1 = cartMapper.selectOne(wrapper);
        if (cart1 != null) {
            cart1.setNumber(cart1.getNumber() - 1);
            if (cart1.getNumber() <= 0) {
                cartMapper.delete(wrapper);

            } else {
                cartMapper.updateById(cart1);
            }
        }
        return cart1;
    }
}
